//
//  SHComposePictureView.swift
//  SHWeibo
//
//  Created by jingming wang on 16/8/3.
//  Copyright © 2016年 cpy.name. All rights reserved.
//

import UIKit

private let reuserIdentifier = "composePictureViewCellId"
class SHComposePictureView: UICollectionView {
    // 定义一个闭包通知vc弹出相册
    var closure: (() -> ())?
    
    // 保存图片数组
    var imageList: [UIImage] = [UIImage]()
    
    override init(frame: CGRect, collectionViewLayout layout: UICollectionViewLayout) {
        super.init(frame: frame, collectionViewLayout: UICollectionViewFlowLayout())
        setupUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    // MARK: - 设置界面
    private func setupUI() {
        backgroundColor = UIColor.clearColor()
        let cellMargin: CGFloat = 5
        let cellWH = (K_SCREEN_WIDTH - 20 - cellMargin*2)/3
        
        let layout = self.collectionViewLayout as! UICollectionViewFlowLayout
        // 设置item
        layout.itemSize = CGSize(width: cellWH, height: cellWH)
        // 设置垂直和水平间距
        layout.minimumInteritemSpacing = cellMargin
        layout.minimumLineSpacing = cellMargin
        // 设置代理
        dataSource  = self
        delegate = self
        registerClass(SHComposePictureViewCell.self, forCellWithReuseIdentifier: reuserIdentifier)
    }
    
    // MARK: - 添加图片
    func addImages(image: UIImage){
        // 添加图片
        imageList.append(image)
        // 显示配图
        hidden = false
        // 刷新UI
        reloadData()
    }
}

// MARK: - dataSource
extension SHComposePictureView: UICollectionViewDataSource, UICollectionViewDelegate {
    func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        if imageList.count == 0 || imageList.count == 9 {
            return imageList.count
        }
        return imageList.count + 1
    }
    
    func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCellWithReuseIdentifier(reuserIdentifier, forIndexPath: indexPath) as! SHComposePictureViewCell
        // 判断是最后一个item 代表要显示加号图片
        if indexPath.item == imageList.count {
            cell.image = nil
        }else {
            cell.image = imageList[indexPath.item]
        }
        
        cell.closure = { [weak self] in
            // 移除指定位置的image
            self?.imageList.removeAtIndex(indexPath.item)
            
            if self?.imageList.count == 0 {
                self?.hidden = true
            }
            // 刷新UI
            self?.reloadData()
        }
        
        return cell
    }
    
    func collectionView(collectionView: UICollectionView, didSelectItemAtIndexPath indexPath: NSIndexPath) {
        collectionView.deselectItemAtIndexPath(indexPath, animated: true)
        
        // 代表点击的为加号按钮
        if indexPath.item == imageList.count {
            closure?()
        }
    }
}

